package com.example.fresh_fruit.controller;

import com.example.fresh_fruit.dto.CustomerDto;
import com.example.fresh_fruit.entity.Customer;
import com.example.fresh_fruit.service.CustomerService;
import com.example.fresh_fruit.util.Result;
import com.example.fresh_fruit.第三方接口.Recharge;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author :飞天の鱼
 * @date : 2022/8/12 11:14
 */
@Slf4j
@RestController
//该注解是@Controller和@ResponseBody的结合体，将响应数据直接塞到响应体里面
@RequestMapping("/customer")//定义uri，接口的访问地址

public class CustomerController {
    @Autowired
    private CustomerService customerService;

    @GetMapping("/querall")
    @ApiOperation(value = "查询所有")
    public List<Customer> querall() {
        log.info("开始查询log");
        System.out.println("开始查询");
        return customerService.selectAll();
    }

    @Transactional
    @PostMapping("/findByName")
    public Customer findByName(@RequestBody String name) {
        return customerService.findByName(name);
    }

    @PostMapping("login")//    登入从前端传来了
    public Result login(@RequestBody Customer para) {

        Customer login = customerService.login(para.getName(), para.getPasswd());
//String Token= TokenUtil.createToken(para.getId());
        if (login == null) {
            return Result.fail().message("登录失败");
        }
        //创建token并返回给前端
//        String token= TokenUtil.createToken(para);
        //登陆成功 返回token——>message().data("token",token)
        //即将响应结果返回给前端
        return Result.ok().message("登录成功");
    }


    @PutMapping("/updata")
    public Result updata(@RequestBody CustomerDto customerDto) {//修改密码
        Integer id = customerDto.getId();
        String userName = customerDto.getName();
        String oldPasswd = customerDto.getOldPasswd();
        String newPasswd = customerDto.getNewPasswd();
        Integer result = customerService.updeCustomer(id, userName, oldPasswd, newPasswd);
//        switch (result) {
//            case 1:
//                return Result.error().message("用户数据不存在");
//            case 2:
//                return Result.error().message("用户数据已被删除");
//            case 3:
//                return Result.error().message("密码错误，请输入正确的密码");
//            case 0:
//                return Result.ok().message("验证通过，更新密码");
//            default:
//        }
        if (result == 1) {
            return Result.fail().message("用户数据不存在");
        } else if (result == 2) {
            return Result.fail().message("用户数据已被删除");
        } else if (result == 3) {
            return Result.fail().message("密码错误，请输入正确的密码");
        } else {
            return Result.ok().message("验证通过，更新密码");
        }
    }

    @PostMapping("/insertCustomer")//注册
    public Result insertCustomer(@RequestBody Customer customer) {//注册
        int result = customerService.insertCustomer(customer);

        if (result == 1) {
            return Result.fail().message("用户名已存在");
        } else {
            return Result.ok().message("注册成功");
        }

    }




    @PostMapping("/Recharge")//伪充值
    public Result insertRecharge(@RequestParam Recharge recharge, @RequestParam Integer id) {
        int banlance = customerService.updataBanlance(recharge, id);
        if (banlance != 0) {
            return Result.ok("OK");
        }
        else {
            return Result.fail("fail");
        }
    }
}
